//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    background: ['cloud://banmanshop.6261-banmanshop-1301943798/banner.png', 'cloud://banmanshop.6261-banmanshop-1301943798/banner.png'],
    indicatorDots: true,
    autoplay: true,
    interval: 2000,
    duration: 500,
    zhuanquimgs: [],
    bannerimgs: [],
    backgroundImage: '../../static/images/tuijianbg.png',
    myProductList: []
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  goToIndex(e) {
    // var cateId=
    wx.navigateTo({
      url: '../search/search?from=index'
    })
  },
  getList: function (id) {
    var that = this
    wx.request({
      url: app.globalData.baseUrl + '/product/getProductByTag?tagCode=' + id,
      success(res) {
        var data = res.data.data.rows
        var productList = []
        // console.log(data)
        for (let i = 0; i < data.length; i++) {
          productList.push(data[i].productList[0])
        }
        for (let i = 0; i < productList.length; i++) {
          productList[i].mainImageUrl = app.globalData.baseUrl + '/getFile?id=' + productList[i].mainImageUrl
        }
        that.setData({
          myProductList: productList
        })
      }
    })
  },
  todelti(e) {
    wx.navigateTo({
      url: '../details/details?cId=' + e.currentTarget.dataset.id
    })
  },
  onLoad: function (query) {
    const scene = decodeURIComponent(query.scene)
    wx.setStorage({
      data: scene,
      key: 'bindKey'
    })
    var that = this
    // 积分专区请求
    wx.request({
      url: app.globalData.baseUrl + '/findAllTag',
      success(res) {
        let data = res.data.data
        let ztc = data.ztc
        let syzjtjw = data.syzjtjw[0].attId
        let keyCode = data.syzjtjw[0].keyCode
        that.getList(keyCode)
        // 设置“每日推荐”背景图
        that.setData({
          backgroundImage: app.globalData.baseUrl + '/getFile?id=' + syzjtjw
        })
        // 设置“积分专区”的信息及图片
        ztc.forEach((item) => {
          item.imgsrc = app.globalData.baseUrl + '/getFile?id=' + item.attId
        })
        that.setData({
          zhuanquimgs: ztc
        })
      }
    })
    // banner图片请求
    wx.request({
      url: app.globalData.baseUrl + '/findAllAdvertising',
      success(res) {
        var data = res.data.data
        data.forEach(item => {
          item.bannerimg = app.globalData.baseUrl + '/getFile?id=' + item.bigImgageId
        })
        that.setData({
          bannerimgs: data
        })
      }
    })
  },
  getUserInfo: function (e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})